package com.ruoyi.car.controller.admin.category;

import com.github.pagehelper.PageInfo;
import com.ruoyi.car.controller.admin.category.vo.CarCategoryAddReq;
import com.ruoyi.car.controller.admin.category.vo.CarCategoryEditReq;
import com.ruoyi.car.controller.admin.category.vo.CarCategoryResp;
import com.ruoyi.car.controller.admin.model.vo.CarModelAddReq;
import com.ruoyi.car.controller.admin.model.vo.CarModelEditReq;
import com.ruoyi.car.controller.admin.model.vo.CarModelResp;
import com.ruoyi.car.entity.CarCategory;
import com.ruoyi.car.entity.CarModel;
import com.ruoyi.car.mapstruct.CarCategoryTo;
import com.ruoyi.car.mapstruct.CarModelConvert;
import com.ruoyi.car.service.CarCategoryService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PageReq;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.MessageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/6/5 14:47
 * @Version 1.0
 */
@Api(tags = "后台管理-商品信息")
@RestController
@RequestMapping(value = "/car/category")
public class CategoryController {
    @Autowired
    private CarCategoryService carCategoryService;

    @PreAuthorize(value = "@ss.hasPermi('car:category:page')")
    @GetMapping(value = "/page")
    @ApiOperation(value = "获取分页列表")
    public TableDataInfo list(PageReq req) {
        PageInfo<CarCategory> pageInfo = this.carCategoryService.search(req);

        //总记录数
        long count= pageInfo.getTotal();
        //转换完成以后的结果
        List<CarCategoryResp> list = CarCategoryTo.INSTANCE.convert(pageInfo.getList());

        TableDataInfo tableDataInfo = new TableDataInfo(list, count);
        return tableDataInfo;
    }

    @Log(title = "商品分类",businessType = BusinessType.INSERT)
    @PreAuthorize(value = "@ss.hasPermi('car:category:add')")
    @ApiOperation(value = "新增")
    @PostMapping
    public R add(@RequestBody @Valid CarCategoryAddReq req){
        CarCategory convert = CarCategoryTo.INSTANCE.convert(req);
        boolean save = this.carCategoryService.save(convert);
        return save? R.ok(): R.fail();
    }

    @PreAuthorize(value = "@ss.hasPermi('car:category:query')")
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = "/{id}")
    public R<CarCategoryResp> findById(@PathVariable(value = "id") Long id){
        CarCategory db = this.carCategoryService.getById(id);
        if(db==null){
            //国际化 i18n 中查询
            String msg= MessageUtils.message("db.not.found");
            throw new ServiceException(msg);
        }

        CarCategoryResp convert = CarCategoryTo.INSTANCE.convert(db);
        return R.ok(convert);
    }

    @Log(title = "商品分类",businessType = BusinessType.UPDATE)
    @PreAuthorize(value = "@ss.hasPermi('car:category:edit')")
    @ApiOperation(value = "修改保存")
    @PutMapping
    public R  update(@RequestBody @Valid CarCategoryEditReq req){
        CarCategory db = this.carCategoryService.getById(req.getId());
        if(db==null){
            //国际化 i18n 中查询
            String msg=MessageUtils.message("db.not.found");
            throw new ServiceException(msg);
        }

        CarCategory convert = CarCategoryTo.INSTANCE.convert(req);

        boolean save = this.carCategoryService.updateById(convert);
        return save? R.ok(): R.fail();
    }
}
